package com.example.config;

import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.ITemplateEngine;
import org.thymeleaf.Thymeleaf;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.StringTemplateResolver;

import java.util.List;

@Configuration
@EnableWebMvc
@ComponentScans(
        {@ComponentScan("com.example.controller"),@ComponentScan("com.example.service"),@ComponentScan("com.example.config")}
)
@MapperScan("com.example.mapper")
public class WebConfiguration implements WebMvcConfigurer {
@Bean
    public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine springTemplateEngine){
        ThymeleafViewResolver resolver=new ThymeleafViewResolver();
        resolver.setOrder(1);
        resolver.setCharacterEncoding("UTF-8");
        resolver.setTemplateEngine(springTemplateEngine);
        return  resolver;
}
@Bean
    public SpringResourceTemplateResolver templateResolver(){
    SpringResourceTemplateResolver resolver=new SpringResourceTemplateResolver();
    resolver.setSuffix(".html");
    resolver.setPrefix("classpath:");
    return resolver;

}
@Bean
public SpringTemplateEngine springTemplateEngine(ITemplateResolver resolver){
    SpringTemplateEngine engine=new SpringTemplateEngine();
    engine.setTemplateResolver(resolver);
    return engine;
}
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();   //开启默认的Servlet
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        //配置静态资源的访问路径
    }
        @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
    converters.add(new FastJsonHttpMessageConverter());
        }
}
